﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BookStore.Solution.BLL;
using BookStore.Solution.Utilities;

namespace BookStore.Solution.Presentation
{
    public enum SearchEmployeeType
    {
        ID,
        Name,
        Position
    }
    public partial class frmSearchEmployee : DevComponents.DotNetBar.OfficeForm
    {
        public DataTable ResultTable = null;
        private SearchEmployeeType type;
        private EmployeeManager manager = new EmployeeManager();
        public frmSearchEmployee(SearchEmployeeType type)
        {
            InitializeComponent();
            txtKeyword.Visible = cbPosition.Visible = false;
            this.type = type;
            Text += type.ToString() + ":";
            switch (type)
            {
                case SearchEmployeeType.ID:
                    Text +="mã nhân viên:";
                    break;
                case SearchEmployeeType.Name:
                    Text +="tên nhân viên:";
                    break;
                case SearchEmployeeType.Position:
                    Text +="chức vụ:";
                    break;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtKeyword.Text == "" && txtKeyword.Visible == true)
            {
                MessageBox.Show("Chưa nhập từ khóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (type)
            {
                case SearchEmployeeType.ID:
                    ResultTable = manager.SearchByID(txtKeyword.Text);
                    break;
                case SearchEmployeeType.Name:
                    ResultTable = manager.SearchbyName(txtKeyword.Text);
                    break;
                case SearchEmployeeType.Position:
                    ResultTable = manager.SearchByPosition(DataUtil.ToInt(cbPosition.SelectedValue));
                    break;
            }

            if (ResultTable.Rows.Count == 0)
            {
                MessageBox.Show("Không tìm thấy nhân viên nào", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ResultTable = null;
                return;
            }
            Close();
        }

        private void frmSearchEmployee_Load(object sender, EventArgs e)
        {
            switch (type)
            {
                case SearchEmployeeType.ID:
                    txtKeyword.Visible = true;
                    break;
                case SearchEmployeeType.Name:
                    txtKeyword.Visible = true;
                    break;
                case SearchEmployeeType.Position:
                    cbPosition.Visible = true;
                    PositionManager manager = new PositionManager();
                    DataTable dt = manager.GetAll();
                    cbPosition.DataSource = dt;
                    cbPosition.ValueMember = "PositionID";
                    cbPosition.DisplayMember = "PositionName";
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("Không có chức vụ nào", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ResultTable = null;
                        Close();
                    }
                    break;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ResultTable = null;
            Close();
        }

        private void Search_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnSearch.PerformClick();
        }
    }
}